CREATE TABLE users
(
    id         NUMBER(19) NOT NULL,
    username   VARCHAR2(100) NOT NULL,
    first_name VARCHAR2(50) NOT NULL,
    last_name  VARCHAR2(50),
    CONSTRAINT pk_users PRIMARY KEY (id),
    CONSTRAINT uk_username UNIQUE (username)
);

-- 创建序列 (Oracle 11g/12c 都能用)
CREATE SEQUENCE users_seq START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE;

-- 创建触发器：插入时自动取序列值
CREATE
OR REPLACE TRIGGER trg_users_id
BEFORE INSERT ON users
FOR EACH ROW
WHEN (NEW.id IS NULL)
BEGIN
SELECT users_seq.NEXTVAL
INTO :NEW.id
FROM dual;
END;